package
{
	import baidu.map.basetype.LngLat;
	import baidu.map.basetype.Size;
	import baidu.map.core.Map;
	import baidu.map.event.ConvertEvent;
	import baidu.map.layer.Layer;
	import baidu.map.layer.RasterLayer;
	import baidu.map.overlay.Marker;
	import baidu.map.service.convert.ConvertType;
	import baidu.map.service.convert.Convertor;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.geom.Point;
	import flash.system.Security;

	public class ConvertExample extends Sprite
	{
		private var _convertor:Convertor;
		private var _map:Map;
		
		public function ConvertExample()
		{
			flash.system.Security.allowDomain("*");
			// 舞台设置 
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			// 创建一个大小为600*400的Map对象
			_map = new Map(new Size(800, 600));
			addChild(_map);
			// 初始化Map的中心点和显示级别
			_map.centerAndZoom(new LngLat(116.404, 39.915), 12);
			
			// 添加底图
			var layer:Layer = new RasterLayer("BaiduMap", _map);
			_map.addLayer(layer);
			
			_convertor = new Convertor();
			addChild(_convertor);
			_convertor.addEventListener(ConvertEvent.CONVERT_SUCCESS, onSuccess);
			_convertor.addEventListener(ConvertEvent.NETWORK_ERROR, onError);
			_convertor.addEventListener(ConvertEvent.BAT_CONVERT_COMPLETE, onBatComplete);
			_convertor.convert(116.32715863448607, 39.990912172420714, ConvertType.GCJ02_TO_BAIDU);	
			
//			var arr:Array = new Array();
//			arr.push(new Point(116.32715863448607, 39.990912172420714));
//			arr.push(new Point(116.30, 39.90));
//			arr.push(new Point(116.32715863448607, 39.990912172420714));
//			arr.push(new Point(116.30, 39.90));
//			arr.push(new Point(116.32715863448607, 39.990912172420714));
//			arr.push(new Point(116.30, 39.90));
//			arr.push(new Point(116.32715863448607, 39.990912172420714));
//			arr.push(new Point(116.30, 39.90));
//			_convertor.batConvert(arr, ConvertType.WGS84_TO_BAIDU);
		}
		
		protected function onSuccess(evt:ConvertEvent):void{
			var obj:Object = evt.data;
			var marker:Marker = new Marker();
			trace(obj.errorid, obj.x, obj.y);
			marker.position = new LngLat(obj.x, obj.y);
			_map.addOverlay(marker);
		}
		
		protected function onError(evt:ConvertEvent):void{
			trace(evt.toString());
		}
		
		protected function onBatComplete(evt:ConvertEvent):void{
			var arr:Array = evt.data as Array;
			for(var i:int = 0, iLen:int = arr.length; i < iLen; i++){
				trace(arr[i].errorid, arr[i].x, arr[i].y);
			}
		}
	}
}